# -*- coding:utf-8 -*-

r"""
redis model
"""

import redis
from framework.model import Model
from config.cfg import db_cfg

class RedisModel(redis.Redis, Model):
    """base redis"""
    def __init__(self, **kwargs):
        """init"""
        c = dict()
        if kwargs:
            c = kwargs
        else:
            c = self.load_db()
        super(RedisModel, self).__init__(**c)

    def load_db(self):
        """load db"""
        for items in db_cfg:
            if 'name' in items.keys() and 'type' in items.keys():
                if items.get('type') == 'redis':
                    pool = redis.ConnectionPool(host=items.get("host"), port=items.get("port"), db=items.get("db"))
                    return dict(connection_pool=pool)
        return

    def switch_db(self, db):
        """switch db"""
